<?php
##################################
# photogals
##################################

# variables
$page['L_message'] = "";
$page['form_action'] = convert_url("index.php?r=" . $plugin_idurl .
    "&v1=form_photogals");
$nb_erreur = "0";
$page['erreur'] = array();
$page['photogals'] = array();

# form values
$page['value_id'] = "";
$page['value_name'] = "";
$page['value_description'] = "";
$page['value_couverture'] = "";
$page['value_file'] = "" ;
$page['photogalsimages'] = array() ;


if ($right_user['add_photogals'] or $right_user['edit_photogals']) {
    $page['show_form'] = 1;
} else {
    $page['show_form'] = '';
    $page['L_message'] = $lang['general']['acces_reserve_admin'];
}

# si l'identifiant du photogals est passe dans l'url (modification), on le recupere
if (isset($_GET['v2']) and $_GET['v2'] != "") {
    $page['value_id'] = $_GET['v2'];
} elseif (isset($_POST['id']) and $_POST['id'] != "") {
    $page['value_id'] = $_POST['id'];
}

# case of add or edit
if (isset($_POST) and !empty($_POST)) {
    # we format datas
    if (isset($_POST['name']))
        $_POST['name'] = format_txt($_POST['name']);

    # we check datas
    if (!isset($_POST['description']) or $_POST['description'] == "") {
        $page['erreur'][$nb_erreur]['message'] = $lang['photogals']['E_empty_description'];
        $nb_erreur++;
    }

    $_POST['description'] = array_key_exists('description', $_POST) ? addslashes($_POST['description']) : '' ;

    # there is no error in submited datas
    if ($nb_erreur == 0) {
        # case : new item to add
        if (!isset($_POST['id']) or empty($_POST['id']) and $right_user['add_photogals']) {

            $sql_add = sql_replace($sql['photogals']['insert_cat'], $_POST);
            $sgbd = sql_connect();
            $execution = sql_query($sql_add);
            if ($execution) {
                $page['L_message'] = $lang['photogals']['form_photogals_add_1'];
            } else {
                $page['L_message'] = $lang['photogals']['form_photogals_add_0'];
            }
            $page['value_id'] = sql_insert_id($sgbd);
            sql_close($sgbd);
        }
        # case : item to modify
        elseif ($right_user['edit_photogals']) {
            $sql_modification = sql_replace($sql['photogals']['edit_cat'], $_POST);
            $sgbd = sql_connect();
            if (sql_query($sql_modification) != false) {
                $page['L_message'] = $lang['photogals']['form_photogals_edit_1'];
            } else {
                $page['L_message'] = $lang['photogals']['form_photogals_edit_0'];
            }
            sql_close($sgbd);
        }
    } else {
        # there is some errors: we show the datas again
        if (isset($_POST['id']))
            $page['value_id'] = $_POST['id'];
        if (isset($_POST['name']))
            $page['value_name'] = $_POST['name'];
        if (isset($_POST['description']))
            $page['value_description'] = $_POST['description'];
        if (isset($_POST['file']))
            $page['value_file'] = $_POST['file'];
    }
}

# if the ID is known, we get the datas to show them in the form
if (isset($page['value_id']) and $page['value_id'] != "" and $nb_erreur == 0) {
    # we get the item information
    $var['id'] = $page['value_id'];
    $sql_details = sql_replace($sql['photogals']['get_cat_details'], $var);

    $sgbd = sql_connect();
    $res = sql_query($sql_details);
    $ligne = sql_fetch_array($res);
    sql_free_result($res);
    sql_close($sgbd);

    $page['value_id'] = $ligne['category_id'];
    $page['value_name'] = $ligne['category_name'];
    $page['value_description'] = $ligne['category_description'];
    $page['value_file'] = $ligne['category_home_img'];
    
    $sql_photogals=sql_replace($sql['photogals']['get_cat_images'],$var);

    $sgbd = sql_connect();
    $res_photogals = sql_query($sql_photogals);
    $nb_ligne=sql_num_rows($res_photogals);
    $i = 0;
    
     while($ligne = sql_fetch_array($res_photogals))
     {
      $page['photogalsimages'][$i]['id']=$ligne['category_id'];
      $page['photogalsimages'][$i]['description']=$ligne['image_description'];
      $page['photogalsimages'][$i]['homeimg']=$ligne['image_name'];  
      $page['photogalsimages'][$i]['mod']=$i%2;
    
      if($right_user['delete_photogals'])
      {
       $page['photogalsimages'][$i]['photogalsimages_delete']=convert_url("index.php?r=".$plugin_idurl."&v1=form_photogals&v2=".$ligne['category_id']."&v4=".$ligne['image_id']);
      }
      $i++;
     }
     
    sql_free_result($res_photogals);
    sql_close($sgbd);

}

if(isset($_GET['v4'])){
    # suppression
    if($right_user['delete_photogals']) 
    {
     $var['id']=$_GET['v4'];
     $sql_sup=sql_replace($sql['photogals']['delete_cat_image'],$var);
     $sgbd = sql_connect();
    
     if(sql_query($sql_sup) != false) { $page['L_message']= 'l\'image a bien &eacute;t&eacute; supprimer'; }
     else { $page['L_message']='Impossible de supprimer l\'image'; }
    
     sql_close($sgbd);
    } else {
        $page['L_message']='vous n\'avez passez le droit de supprimer cet element!!';
    }
}

# photogalss
if ($right_user['delete_photogals'] and !empty($page['value_id'])) {
    $page['photogals_delete'] = convert_url("index.php?r=" . $plugin_idurl .
        "&v1=photogals_list&v2=delete&v3=" . $page['value_id']);

} else {
    $page['photogals_delete'] = "";
}
$page['photogals_list'] = convert_url("index.php?r=" . $plugin_idurl .
    "&v1=photogals_list");

# elements du formulaire
# liste des images
$sgbd = sql_connect();
$sql_liste = $sql['photogals']['get_cat_images'];
$res_liste = sql_query($sql_liste);
$i = "0";
while ($ligne = sql_fetch_array($res_liste)) {
    $page['image'][$i]['value_image_name'] = $ligne['image_name'];
    $page['type'][$i]['value_image_description'] = $ligne['image_description'];
    $i++;
}
sql_free_result($res_liste);
sql_close($sgbd);


/*
    -------------------------------------------
    Traitement ajuout d'image dans une catégorie'
    -------------------------------------------
*/

if(isset($_POST['addimage']) && $_POST['addimage'] == 1){

    # we check datas
    if (!isset($_POST['phototoadd']) or $_POST['phototoadd'] == "") {
        $error = 'Veuillez choisir la photo &agrave; rajouter';
        die($error);
    }

    $_POST['imagedescription'] = addslashes($_POST['imagedescription']);
    
    $sql_add_image = sql_replace($sql['photogals']['insert_cat_image'], $_POST);
    $sgbd = sql_connect();
    $execution = sql_query($sql_add_image);
    if ($execution) {
        die('1');
    } else {
        $error = 'Erreur lors de l\'ajout de la photo. Re&eacute;ssayer SVP!';
        die($error);
    }
    sql_close($sgbd);
}
/*
    -------------------------------------------
    Traitement ajuout d'image dans une cat&eacute;gorie'
    -------------------------------------------
*/

$page['link_choose_image']=convert_url("index.php?r=".$lang['general']['idurl_file']."&v1=image_manager&parent_form=form_photogals&field_name=file&file_type=image&fen=pop",0);
$page['link_choose_image_to_add'] = convert_url("index.php?r=".$lang['general']['idurl_file']."&v1=image_manager&parent_form=addImageForm&field_name=file2&file_type=image&fen=pop",0);
# text
if (empty($page['value_id'])) {
    $page['L_title'] = $lang['photogals']['form_photogals_add'];
} else {
    $page['L_title'] = $lang['photogals']['form_photogals_edit'];
}
$page['L_valider'] = $lang['photogals']['submit'];
$page['L_delete'] = $lang['photogals']['delete'];
$page['L_back_list'] = $lang['photogals']['back_list'];
$page['L_choose_position'] = $lang['photogals']['choose_position'];
$page['L_choose_type'] = $lang['photogals']['choose_type'];
$page['L_url'] = $lang['photogals']['URL'];
$page['L_pub_date'] = $lang['photogals']['publish_date'];
$page['L_end_date'] = $lang['photogals']['end_date'];
$page['L_width'] = $lang['photogals']['width'];
$page['L_height'] = $lang['photogals']['height'];
$page['L_choose_statut'] = $lang['photogals']['choose_statut'];
$page['L_choose_image'] = $lang['photogals']['choose_image'];
$page['L_file'] = $lang['photogals']['file'];

$page['L_erreur'] = $lang['general']['E_erreur'];
$page['L_field_required'] = $lang['general']['field_required'];

$page['L_name'] = $lang['photogals']['name'];
$page['L_position'] = $lang['photogals']['position'];
$page['L_description'] = $lang['photogals']['description'];
$page['L_status'] = $lang['photogals']['status'];

$page['meta_title'] = $page['L_title'];
$page['template'] = $tpl['photogals']['form_photogals'];
?>